/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.gameobjects.player.PlayerScripts;
import com.aionengine.gameserver.model.house.House;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_HOUSE_SCRIPTS;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author Rolandas
 */
public class CM_HOUSE_SCRIPT extends AionClientPacket {

    int address;
    int scriptIndex;
    int totalSize;
    int compressedSize;
    int uncompressedSize;
    byte[] stream;

    public CM_HOUSE_SCRIPT(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    @Override
    protected void readImpl() {
        address = readD();
        scriptIndex = readC();
        totalSize = readH();
        if (totalSize > 0) {
            compressedSize = readD();
            if (compressedSize < 8150) {
                uncompressedSize = readD();
                stream = readB(compressedSize);
            }
        }
    }

    @Override
    protected void runImpl() {
        Player player = getConnection().getActivePlayer();

        if (compressedSize > 8149) {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_HOUSING_SCRIPT_OVERFLOW);
        }

        House house = player.getActiveHouse();
        if (house == null)
            return;

        PlayerScripts scripts = house.getPlayerScripts();

        if (totalSize <= 0) {
            // Deposit perhaps should send 0, while delete -1
            // But the client sends the same packets now
            scripts.addScript(scriptIndex, new byte[0], 0);
        } else {
            scripts.addScript(scriptIndex, stream, uncompressedSize);
        }

        PacketSendUtility.sendPacket(player, new SM_HOUSE_SCRIPTS(address, scripts, scriptIndex, scriptIndex));
    }

}
